<?php
namespace Services;
/**
 * File holds class for feedback data manipulation
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Services
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Feedback extends ORM {
	/**
	 * Method parses input and then stores to database. When returning user
	 * get user ID, when new user, make new entry to database
	 * @param array $post HTML POST array
	 * @return int ID of insert when successful, 0 when problems
	 */
	public function prepareSave($post) {
		$id = 0;
		if(isset($post) && count($post) > 0) {
			$feedback = new \Entities\Feedback();
			$feedback->setBusiness((int)$post['business']);
			$feedback->setComment(self::_cleanInput($post['comment']));
			$feedback->setRating((int)$post['score']);
			$feedback->setTime(time());
			
			//determine returning user
			$userService = new \Services\User();
			$user = $userService->findUserByEmailOrPhone($post['email'], $post['phone']);
			//new user? then insert to db
			if (!$user && (strlen($post['email']) > 0 || strlen($post['phone'])) > 0) {
				$uid = $userService->prepareSave($post);
			} else if ($user) {
				$uid = $user->getId();
			} else {
				$uid = 0;
			}
			$feedback->setUser($uid);
			try {
				$id = $this->insert($feedback);
			} catch (\Exception $e) {
				//TODO: do not eat exception
				$id = 0;
			}
		}
		return $id;
	}
	
	public function readByBusiness(\Entities\Business $business) {
		//TODO:what is not completed here ?
	}
	
	/**
	 * Method searches for comments by rater type and time
	 * @param unknown_type $detractors
	 * @param unknown_type $passives
	 * @param unknown_type $promoters
	 * @param unknown_type $from
	 * @param unknown_type $to
	 * @param unknown_type $business
	 * @return Ambigous <boolean, object>
	 */
	public function commentSearch($detractors, $passives, $promoters, $from, $to, $business) {
		$out = false;
		$raters = array();
		if ($detractors > 0) {
			$raters[] = ' `rating` BETWEEN ' . \Enum\NPS::Detractors . ' AND ' . (\Enum\NPS::Passive - 1);
		}
		if ($passives > 0) {
			$raters[] = ' `rating` BETWEEN ' . \Enum\NPS::Passive . ' AND ' . (\Enum\NPS::Promoters - 1);
		}
		if ($promoters > 0) {
			$raters[] = ' `rating` BETWEEN ' . \Enum\NPS::Promoters . ' AND 10';
		}
		$sql = 'SELECT * FROM `feedback` where `time` BETWEEN %s AND %s AND `business` = %d';
		$appendix = '';
		if (count($raters) > 0) {
			$appendix = ' AND (' . implode(' OR ', $raters) . ')';
		}
		try {
			//add 1 day to 'to' date, so that user can search comments for 1 day only:
			//etc: from 16.01.2012 to 16.01.2012 will show comments for that day
			$preparedSql = sprintf($sql, strtotime($from), strtotime('+1 day', strtotime($to)), $business);
			$preparedSql .= $appendix;
			$out = $this->rawSelect($preparedSql, new \Entities\Feedback());
		} catch (\Exception $e) {
			//TODO: do not eat
		}
		return $out;
	}
	
	/**
	 * Method removes tags, escapes quotes and removes whitespaec
	 * @param var $str unescaped string
	 * @return var Escaped string
	 */
	private static function _cleanInput($str) {
		$out = strip_tags($str);
		$out = addslashes($out);
		$out = trim($out);
		return $out;
	}
}